import Vue from 'vue';
import App from './App.vue';
import router from './router';
import './style/normalize.css';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios';
import echarts from 'echarts';
import html2Canvas from 'html2canvas';
import JsPDF from 'jspdf';

Vue.prototype.$to = function(arriveName: string, data?: object) {
  this.$router.push({ name: arriveName, query: data });
};

Vue.prototype.$getPdf = function(domName: any, title?: string) {
  let block = document.querySelector(domName) as HTMLElement;
  html2Canvas(block, {
    allowTaint: true
  }).then(function(canvas: any) {
    let contentWidth = canvas.width;
    let contentHeight = canvas.height;
    let pageHeight = (contentWidth / 595) * 842;
    let leftHeight = contentHeight;
    let position = 0;
    let imgWidth = 595;
    let imgHeight = (595 / contentWidth) * contentHeight;
    let pageData = canvas.toDataURL('image/jpeg', 1.0);
    let PDF = new JsPDF('', 'pt', 'a4');
    if (leftHeight < pageHeight) {
      PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
    } else {
      while (leftHeight > 0) {
        PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight);
        leftHeight -= pageHeight;
        position -= 842;
        if (leftHeight > 0) {
          PDF.addPage();
        }
      }
    }
    PDF.save(title ? title : 'PDF导出' + '.pdf');
  });
};

Vue.prototype.$http = axios;
Vue.prototype.$echarts = echarts;

Vue.use(ElementUI);

Vue.config.productionTip = false;

axios.defaults.baseURL = 'http://api.pingda.xiaoxxx.com/';
Vue.prototype.$baseUrl = 'http://api.pingda.xiaoxxx.com/';

// Vue.use(htmlToPdf);
axios.interceptors.request.use(function(config: any) {
  if (sessionStorage.token) {
    config.headers.Authorization = sessionStorage.token;
  }
  return config;
});

axios.interceptors.response.use(
  // -2是重新登录提示
  res => {
    // return res
    if (res.data.code !== 1) {
      if (res.data.code === -1) {
        Vue.prototype.$notify({
          title: '提示',
          message: res.data.message,
          type: 'warning',
          duration: 4500
        });
        router.replace({
          path: 'login'
        });
      } else if (res.data.code === -2 && res.data.message === '密码错误') {
        localStorage.removeItem('token');
        router.replace({ name: 'login' });
        Vue.prototype.$notify({
          title: '提示',
          message: res.data.message,
          type: 'warning',
          duration: 4500
        });
      } else if (res.data.message) {
        Vue.prototype.$notify({
          title: '提示',
          message: res.data.message,
          type: 'warning',
          duration: 4500
        });
      } else {
        Vue.prototype.$notify({
          title: '提示',
          message: '对不起,内部服务器错误',
          type: 'warning',
          duration: 4500
        });
      }
    }
    return res;
  },
  err => {
    Vue.prototype.$notify({
      title: '提示',
      message: '对不起,内部服务器错误',
      duration: 4500
    });
    return Promise.reject(err);
  }
);

router.beforeEach((to, from, next) => {
  // to and from are both route objects
  if (to.name !== 'login') {
    if (!sessionStorage.getItem('token')) {
      next({ name: 'login' });
    } else {
      next();
    }
  } else {
    next();
  }
  // next();
});

new Vue({
  router,
  render: h => h(App)
}).$mount('#app');
